package com.k2.gulimall.ware.service.impl;

import com.k2.common.utils.R;
import com.k2.gulimall.ware.feign.ProductFeignService;
import com.k2.gulimall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.Query;

import com.k2.gulimall.ware.dao.WareSkuDao;
import com.k2.gulimall.ware.entity.WareSkuEntity;
import com.k2.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {


    @Autowired
    WareSkuDao WareSkuDao;

    @Autowired
    ProductFeignService ProductFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> WareSkuEntity = new QueryWrapper<WareSkuEntity>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            WareSkuEntity.eq("sku_id", skuId);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            WareSkuEntity.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                WareSkuEntity
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Integer skuNum, Long skuId, Long wareId) {
        //判断如果没有库存
        List<WareSkuEntity> wareSkuEntities =
                WareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities.size() == 0 || wareSkuEntities == null) {
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            //远程查询sku的名字,如果失败无需回滚
            try {
                R info = ProductFeignService.info(skuId);
                if (info.getCode() == 0) {
                    Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
            }
            WareSkuDao.insert(skuEntity);
        } else {
            WareSkuDao.addStock(skuNum, skuId, wareId);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVo SkuHasStockVo = new SkuHasStockVo();
            //查询当前sku的库存量
            //select sum(stock-stock_locked) from wms_ware_sku where sku_id = 1;
            Long count = baseMapper.getSkuStock(skuId);
            SkuHasStockVo.setSkuId(skuId);
            SkuHasStockVo.setHasStock(count==null?false:count>0);
            return SkuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}